package com.crazy.util;

import com.alibaba.fastjson.JSONObject;
import com.crazy.entity.IpInfoEntity;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Location;
import com.maxmind.geoip2.record.Subdivision;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;

/**
 * @author panyonghui
 * @describe ip地址获取工具类
 * @date 2022/2/16 11:29
 */
@Slf4j
public class IpAddressUtils {

    public static DatabaseReader databaseReader;
    static{
        try {
            File dataBase = ResourceUtils.getFile("classpath:GeoLite2-City.mmdb");
            databaseReader = new DatabaseReader.Builder(dataBase).build();
        } catch (FileNotFoundException e) {
            log.error("未找到文件", e);
        } catch (IOException e) {
            log.error("IO异常", e);
        }
    }

    /**
     * @describe 从https://ip-api.com/获取ip信息
     * @auther crazy_cat
     * @date 2022/2/16 15:49
     * @param
     * @return IpInfoEntity
     * @modify
     */
    public static IpInfoEntity ipInfoByIpApi(String ipAddress){
        String url = "http://ip-api.com/json/" + ipAddress + "?fields=61439&lang=zh-CN";
        JSONObject jsonObject = HttpUtils.requestByRestTemplate(url, new HashMap());
        IpInfoEntity ipInfoEntity = jsonObject.toJavaObject(IpInfoEntity.class);
        // 经度
        ipInfoEntity.setLongitude(jsonObject.getDouble("lon"));
        // 纬度
        ipInfoEntity.setLatitude(jsonObject.getDouble("lat"));
        // 国家简称
        ipInfoEntity.setCountryAbb(jsonObject.getString("countryCode"));
        // 省份简称
        ipInfoEntity.setRegionNameAbb(jsonObject.getString("region"));
        return ipInfoEntity;
    }

    /**
     * @describe 从本地资源文件获取ip信息
     * @auther crazy_cat
     * @date 2022/2/16 15:49
     * @param ipAddress
     * @return IpInfoEntity
     * @modify
     */
    public static IpInfoEntity ipInfoByLocal(String ipAddress) throws IOException, GeoIp2Exception {
        InetAddress inetAddress = InetAddress.getByName(ipAddress);
        // 获取查询结果
        CityResponse cityResponse = databaseReader.city(inetAddress);

        // 获取国家信息
        Country country = cityResponse.getCountry();
        // 国家简称
        String countryAbb = country.getIsoCode();
        // 国家英文名称
        String countryNameEn = country.getName();
        // 国家中文名称
        String countryName = country.getNames().get("zh-CN");
        // 获取省份
        Subdivision subdivision = cityResponse.getMostSpecificSubdivision();
        // 省份简称
        String regionNameAbb = subdivision.getIsoCode();
        // 省份英文名称
        String regionNameEn = subdivision.getName();
        // 省份中文名称
        String regionName = subdivision.getNames().get("zh-CN");

        // 获取城市
        City city = cityResponse.getCity();
        // 城市英文名称
        String cityNameEn = city.getName();
        // 城市中文名称
        String cityName = city.getNames().get("zh-CN");

        // 获取经纬度
        Location location = cityResponse.getLocation();
        // 纬度
        Double latitude = location.getLatitude();
        // 经度
        Double longitude =location.getLongitude();

        IpInfoEntity ipInfoEntity = new IpInfoEntity();
        ipInfoEntity.setCountryAbb(countryAbb);
        ipInfoEntity.setCountryEn(countryNameEn);
        ipInfoEntity.setCountry(countryName);
        ipInfoEntity.setRegionNameAbb(regionNameAbb);
        ipInfoEntity.setRegionNameEn(regionNameEn);
        ipInfoEntity.setRegionName(regionName);
        ipInfoEntity.setCityEn(cityNameEn);
        ipInfoEntity.setCity(cityName);
        ipInfoEntity.setLatitude(latitude);
        ipInfoEntity.setLongitude(longitude);
        return ipInfoEntity;
    }
}
